//
//  ShopsClassView.m
//  何五路
//
//  Created by _ADY on 15/7/10.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "ShopsClassView.h"

@implementation ShopsClassView
@synthesize ShopsLArray,shopsId,classId,aLArray;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [UIColor colorWithCIColor:[CIColor colorWithString:@"f8f9fd"]]
        self.backgroundColor = [UIColor colorWithRed:248/255.0 green:249/255.0 blue:253/255.0 alpha:1];
        
        vFrame = CGRectMake(0, 20, frame.size.width, frame.size.height-20);
        
        myTableView = [[UITableView alloc] initWithFrame:vFrame];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor clearColor];
        [self addSubview:myTableView];
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
        [myTableView setTableFooterView:v];
        
        
        if (ShopsLArray == nil) {
            ShopsLArray = [[NSMutableArray alloc] init];
            [self performSelector:@selector(afterShops) withObject:self afterDelay:.5];
        }
        else
            [myTableView reloadData];
        
        selectInt = 0;
        selectCount = 0;
        
    }
    return self;
}

-(void)afterShops
{
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:shopsId forKey:@"shopId"];
    [postDic setObject:@"2" forKey:@"levelsToLoad"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret = [SendService GetShopCategoriesById:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            ShopsLArray =  [ret objectForKey:@"Entity"];
            NSLog(@"店铺分类接口 %@",ret);
            [myTableView reloadData];
            
        });
    });
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return ShopsLArray.count+1+selectCount;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"search";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
    }
    cell.backgroundColor = [UIColor clearColor];
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    
    cell.textLabel.textAlignment = 0;
    cell.textLabel.textColor = [UIColor grayColor];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"全部商品";
    }
    else
    {

        if (indexPath.row > selectInt+selectCount)
        {
            cell.textLabel.text = [NSString stringWithFormat:@"%@",[[ShopsLArray objectAtIndex:indexPath.row-1-selectCount] objectForKey:@"Name"]];
        }
        else if (indexPath.row <= selectInt+selectCount && indexPath.row > selectInt)
        {
            cell.textLabel.text = [NSString stringWithFormat:@"--%@",[[aLArray objectAtIndex:indexPath.row-selectInt-1] objectForKey:@"Name"]];
            cell.backgroundColor = bgColor;
        }
        else
            cell.textLabel.text = [NSString stringWithFormat:@"%@",[[ShopsLArray objectAtIndex:indexPath.row-1] objectForKey:@"Name"]];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.row == 0) {
        classId = @"0";
        [self gotoA];
    }
    else
    {
        
        if (indexPath.row > selectInt+selectCount)
        {
            aLArray = [[ShopsLArray objectAtIndex:indexPath.row-1-selectCount] objectForKey:@"SubVendorCategories"];
                if (aLArray.count >0) {
                selectInt = indexPath.row-selectCount;
                selectCount = aLArray.count;
                [myTableView reloadData];
            }
            else
            {
                classId = [NSString stringWithFormat:@"%@",[[ShopsLArray objectAtIndex:indexPath.row-1-selectCount] objectForKey:@"CategoryId"]];
                [self gotoA];
            }
            
        }
        else if (indexPath.row == selectInt && selectCount != 0)
        {
            selectCount = 0;
            selectInt = indexPath.row;
            [myTableView reloadData];
        }
        else if (indexPath.row <= selectInt+selectCount && indexPath.row > selectInt)
        {
            classId = [NSString stringWithFormat:@"%@",[[aLArray objectAtIndex:indexPath.row-selectInt-1] objectForKey:@"CategoryId"]];
            [self gotoA];

        }
        else
        {
            aLArray = [[ShopsLArray objectAtIndex:indexPath.row-1] objectForKey:@"SubVendorCategories"];
            if (aLArray.count >0) {
                selectCount = aLArray.count;
                selectInt = indexPath.row;
                [myTableView reloadData];
            }
            else
            {
                classId = [NSString stringWithFormat:@"%@",[[ShopsLArray objectAtIndex:indexPath.row-1] objectForKey:@"CategoryId"]];
                [self gotoA];
            }
            
        }
    }
}

-(void)gotoA
{
    [self.delegate JKSshow:classId];
//    NSLog(@"classId %@",classId);
}

@end
